package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.formation.Formation;
import com.xcity.game.player.PlayerUtils;
import com.xcity.game.player.robot.Robot;
import com.xcity.game.player.robot.RobotUtils;
import com.xcity.game.skill.SkillUtils;
import com.xcity.pomelo.response.EmployeeInfoResponse.EmployeeSimpleInfo;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class ViewPlayerResponse extends AbstractViewPlayerResponse {

	public int combatPower; // 总战斗力（包含阵型上的员工）
	public int charm; // 魅力
	public int buildNum; // 拥有建筑数
	public CombatAttributeResponse attr; // 战斗属性
//	public SkillV2ToV1 skill; // 主动技能
	public List<SkillV2Info> skills2;
	public List<EmployeeSimpleInfo> formation = Collections.emptyList(); // 出战阵型
	public int qualityLvl;
	public int destinyLvl;
	public int captureCount;
	
	// rubbish code for client
	public byte gender;
	public int lv;
	public String roleName;
	
	public ViewPlayerResponse(int serial, Player player) {
		super(serial, player);
		if (RobotUtils.isRobot(player)) {
			doInit((Robot) player);
		} else {
			doInit(player);
		}
	}
	
	private void doInit(Player player) {
		this.combatPower = CombatUtils.getCombatPower(player, player.getFormations().get(Formation.TYPE_NORMAL).getUnits());
		this.charm = PlayerUtils.getCharm(player);
		this.buildNum = player.getBuildings2().size();
		this.attr = new CombatAttributeResponse(player);
		this.skills2 = SkillUtils.toResponse(player);
		Employee[] emps = player.getFormations().get(Formation.TYPE_NORMAL).getUnits();
		if (emps != null && emps.length > 0) {
			this.formation = new ArrayList<>(emps.length);
			for (Employee e : emps) {
				this.formation.add(e != null ? new EmployeeSimpleInfo(e) : EmployeeSimpleInfo.EMPTY);
			}
		}
		this.qualityLvl = player.getQualityLvl();
		this.destinyLvl = player.getDestinyLvl();
		this.captureCount = player.getConquests().size();
		
		this.gender = player.getSex();
		this.lv = player.getLevel();
		this.roleName = player.getName();
	}

	private void doInit(Robot robot) {
		this.combatPower = CombatUtils.getCombatPower(robot, null);
		this.charm = 0;
		this.buildNum = robot.getBuildings2().size();
		this.attr = new CombatAttributeResponse(robot);
		this.skills2 = SkillUtils.toResponse(robot);
		// formation empty
		this.qualityLvl = robot.getQualityLvl();
		this.destinyLvl = robot.getDestinyLvl();
		this.captureCount = 0;
		
		this.gender = robot.getSex();
		this.lv = robot.getLevel();
		this.roleName = robot.getName();
	}
}
